﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataTier;
using System.Data.SqlClient;
using System.Data.Common;

namespace DbLayer
{
    class DbPartReservation : DbObject, IDbPartReservation
    {

        private static DbPartReservation _instance = null;
        private IDbStation _dbStation = DbStation.Instance;

        private DbPartReservation()
        {
        }

        public static IDbPartReservation Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new DbPartReservation();
                }
                return _instance;
            }
        }

        public void AddPartReservation(PartReservation partReservation)
        {
            string statement = BuildInsertStatement("PartReservation", partReservation.PickUpDate.ToString(), partReservation.ExpirationDate.ToString());
            ExecuteStatement(statement);
        }

        public IList<PartReservation> GetPartReservations(int reservationNr)
        {

            string statement = BuildSelectStatement("PartReservation", "reservationNr ='" + reservationNr.ToString() + "'");
            SqlDataReader reader = ExecuteQuery(statement);

            List<PartReservation> partReservations = new List<PartReservation>();

            while (reader.Read())
            {
                partReservations.Add(BuildPartReservation(reader));
            }

            reader.Close();

            return partReservations;

        }

        public void RemovePartReservations(int reservationNr)
        {
            string statement = BuildDeleteStatement("PartReservation", "reservationNr = " + reservationNr.ToString());
            ExecuteStatement(statement);
        }

        private PartReservation BuildPartReservation(SqlDataReader reader)
        {

            PartReservation partReservation = new PartReservation();

            partReservation.PickUpDate = Convert.ToDateTime(reader["pickUpDate"]);
            partReservation.ExpirationDate = Convert.ToDateTime(reader["expirationDate"]);
            partReservation.Station = _dbStation.GetStation(Convert.ToInt32(reader["stationId"]));

            return partReservation;


        }
    }
}
